package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.ExamClassifyEntity;
import com.tfjybj.itoo.exam.model.ExamClassifyModel;
import com.tfjybj.itoo.exam.model.ExamClassifyTreeModel;
import com.tfjybj.itoo.exam.provider.service.ExamClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * ExamClassifyController
 * examClassify表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Api(tags = {"examClassify表接口"})
@RequestMapping(value = "/examClassify")
@RestController
public class ExamClassifyController {

    @Resource
    private ExamClassifyService examClassifyService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model ExamClassifyModel
     * @return 添加的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody ExamClassifyModel model) {
		if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        ExamClassifyEntity examClassifyEntity = new ExamClassifyEntity();
        BeanUtils.copyProperties(model, examClassifyEntity);
        examClassifyService.save(examClassifyEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        examClassifyService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        examClassifyService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model ExamClassifyModel
     * @return 修改后的结果
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id修改examClassify")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody ExamClassifyModel model) {
		if (StringUtils.isEmpty(model.getName())){
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        ExamClassifyEntity examClassifyEntity = new ExamClassifyEntity();
        BeanUtils.copyProperties(model, examClassifyEntity);
        examClassifyService.updateById(examClassifyEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找ExamClassify
     *
     * @param id 主键id
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        ExamClassifyEntity examClassifyEntity = examClassifyService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examClassifyEntity);
    }

    /**
     * 分页查询所有ExamClassify
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "分页查询所有ExamClassify")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
								   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<ExamClassifyEntity> examClassifys = examClassifyService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examClassifys);
    }
	
	/**
     * 根据名称模糊查询examClassify
     *
     * @param name  名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的examClassify
     * @author 任晓帅
     * @since 2.0.0 2018-11-15 19:40:28
     */
    @ApiOperation(value = "根据名称模糊查询examClassify", notes = "分页根据名称模糊查询examClassify")
    @GetMapping(value = "queryExamClassifyByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryExamClassifyByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                                  @ApiParam(name = "pageNo",value = "页码",required = true,example = "1") @PathVariable Integer pageNo,
                                                  @ApiParam(name = "pageSize",value = "页数",required = true,example = "10") @PathVariable Integer pageSize) {
        PageInfo<ExamClassifyEntity> examClassifyList = examClassifyService.queryByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", examClassifyList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */
    /**
     * 查询全部examClassify
     *
     * @return 查询全部的examClassify
     * @param operatorId 登陆人id
     * @author 马莹
     * @since 2.0.0 2018-11-23 19:40:28
     */
    @ApiOperation(value="查询全部examClassify",notes="查询全部examClassify")
    @GetMapping(value = "/queryExamClassifyAll/{operatorId}")
    public ItooResult queryExamClassifyAll(@PathVariable String operatorId) {
        List<ExamClassifyTreeModel> examClassifyEntitylist = examClassifyService.queryExamClassifyAll(operatorId);
       return ItooResult.build(ItooResult.SUCCESS,"查询成功",examClassifyEntitylist);
    }

    /**
     * 树形结构点击添加事件
     *
     * @param examTreeModel 树形实体
     * @return 返回受影响行数
     * @author maying
     * @since 2018-11-27 21:43:06
     */
    @ApiOperation(value = "树形结构点击添加事件")
    @PostMapping(value = "/addTree")
    public ItooResult addTree(@RequestBody ExamClassifyTreeModel examTreeModel) {
        if (examTreeModel == null) {
            return ItooResult.build(ItooResult.FAIL, "model不能为空！");
        }
        if (StringUtils.isEmpty(examTreeModel.getParentId())) {
            return ItooResult.build(ItooResult.FAIL, "父id不能为空！");
        }
        if (StringUtils.isEmpty(examTreeModel.getName())) {
            return ItooResult.build(ItooResult.FAIL, "名称不能为空！");
        }
        try {
            return ItooResult.build(ItooResult.SUCCESS, "添加成功！", examClassifyService.saveExamCalssify(examTreeModel));
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "添加失败！");
        }
    }

    /**
     * 树形结构点击编辑事件
     *
     * @param examTreeModel 树形实体
     * @return 返回受影响行数
     * @author maying
     * @since 2018-11-27 21:43:06
     */
    @ApiOperation(value = "树形结构点击编辑事件")
    @PostMapping(value = "/editTree")
    public ItooResult editTree(@RequestBody ExamClassifyTreeModel examTreeModel) {
        if (examTreeModel == null) {
            return ItooResult.build(ItooResult.FAIL, "model不能为空！");
        }
        if (StringUtils.isEmpty(examTreeModel.getId())) {
            return ItooResult.build(ItooResult.FAIL, "id不能为空！");
        }
        if (StringUtils.isEmpty(examTreeModel.getParentId())) {
            return ItooResult.build(ItooResult.FAIL, "父id不能为空！");
        }
        if (StringUtils.isEmpty(examTreeModel.getName())) {
            return ItooResult.build(ItooResult.FAIL, "名称不能为空！");
        }
        try {
            return ItooResult.build(ItooResult.SUCCESS, "更新成功！", examClassifyService.updateExamCalssify(examTreeModel));
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "更新失败！");
        }
    }

    /**
     * 根据id删除树结构
     *
     * @param treeId 选中树形id
     * @return 受影响行数
     * @author maying
     * @since 2018-11-28 11:11:11
     */
    @ApiOperation(value = "根据id删除，tree")
    @DeleteMapping(value = "/treeDelete/{treeId}")
    public ItooResult treeDelete(@PathVariable String treeId) {
        if (StringUtils.isEmpty(treeId)) {
            return ItooResult.build(ItooResult.FAIL, "删除树id不能为空！");
        }
        try {
            return ItooResult.build(ItooResult.SUCCESS, "删除成功！", examClassifyService.treeDelete(treeId));
        } catch (Exception e) {
            return ItooResult.build(ItooResult.FAIL, "删除失败！");
        }
    }
}    
